import http_tls
import flowtool
import utility
import sites_plugin

test_base_floder_path = '/home/dev/Desktop/capreader_trace'
output_file_path = "/home/dev/Desktop/missed_http_tls_tcp_result.txt"

"""

http_tls.py test cases

"""
pkt_log_file_path = test_base_floder_path + "/trace.pcap.pkt.log"
flow_log_file_path = test_base_floder_path + "/trace.pcap.flow.log"
pkt_log_file_folder = test_base_floder_path

def test_missed_http_tls():
    try:
        ht = http_tls.HttpTls()
        ht.missed_http_tls(pkt_log_file_path)
    except:
        print "FAILED http_tls:\tmissed_http_tls()"
        raise
# test_missed_http_tls()

def test_missed_tcp():
    try:
        ht = http_tls.HttpTls()
        ht.missed_tcp(flow_log_file_path)
    except:
        print "FAILED http_tls:\tmissed_tcp()"
        raise
# test_missed_tcp()

def test_missed_http_tls_tcp_folder():
    try:
        ht = http_tls.HttpTls()
        ht.missed_http_tls_tcp_folder(pkt_log_file_folder, output_file_path)
    except:
        print "FAILED http_tls:\tmissed_http_tls_flder()"
        raise
# test_missed_http_tls_tcp_folder()

"""

sites_plugin.py test cases

"""
guid_def_sample = '#define GUID_CPXINTAC    "CPXINTAC"    /* CPX Interactive Ads/Analytics site */'
rule_sample = '{ "HTTP", "Host", "*.daum-img.net", GUID_DAUM, 0 },'

def test_valid_guid_definition():
    try:
        sites = sites_plugin.SitesPlugin()
        if not (sites.valid_guid_definition(guid_def_sample)):
            print "FAILED sites_plugin:\tvalid_guid_definition()"
    except:
        print "FAILED sites_plugin:\tvalid_guid_definition()"
        raise
# test_valid_guid_definition()

def test_valid_rule():
    try:
        sites = sites_plugin.SitesPlugin()
        if not (sites.valid_rules(rule_sample)):
            print "FAILED sites_plugin:\ttest_valid_rule()"
    except:
        print "FAILED sites_plugin:\ttest_valid_rule()"
        raise
# test_valid_rule()

def test_format_guid():
    try:
        sites = sites_plugin.SitesPlugin()
        sample = "GUID_AAAAA"
        sample1 = "AAAAA"
        if sites.format_guid(sample) != "GUID_AAAAA" or sites.format_guid(sample1) != "GUID_AAAAA":
            print "FAILED sites_plugin:\tformat_guid()"
    except:
        print "FAILED sites_plugin:\tformat_guid()"
        raise
#test_format_guid()

def test_parse_rule():
    try:
        """
        { "HTTP", "Host", "naver.com", GUID_NAVER, 0 }, --> ['Host', 'naver.com', 'y', 'n', 'GUID_NAVER']
        """
        sites = sites_plugin.SitesPlugin()
        sample = '{ "HTTP", "Host", "naver.com", GUID_NAVER, 0 },'
        sample_list = []
        sample_list.append("Host")
        sample_list.append("naver.com")
        sample_list.append("y")
        sample_list.append("n")
        sample_list.append("GUID_NAVER")
        result_list = sites.parse_rule(sample)
        if len(result_list) == 0 :
            print "FAILED sites_plugin:\tparse_rule" 
        for i in range(len(result_list)):
            if result_list[i] != sample_list[i]:
                print "FAILED sites_plugin:\tparse_rule" 
    except:
        print "FAILED sites_plugin:\tparse_rule()"
        raise
# test_parse_rule()

def test_make_webpattern_sites():
    try:
        sites = sites_plugin.SitesPlugin()
        rule_f = "/home/dev/Desktop/all_sites_rules.txt"
        guid_f = "/home/dev/Desktop/all_guids.txt"
        sites.make_webpattern_sites(guid_f, rule_f)
    except:
        print "FAILED sites_plugin:\tmake_webpattern_no_foi()"
        raise
# test_make_webpattern_sites()

def test_make_webpattern_ads_sites():
    try:
        sites = sites_plugin.SitesPlugin()
        rule_f = "/home/dev/Desktop/all_referer_rules.txt"
        guid_f = "/home/dev/Desktop/all_guids.txt"
        output_f = "/home/dev/Desktop/webpattern_ads_sites.txt"
        sites.make_webpattern_ads_sites(rule_f, guid_f, output_f)
    except:
        print "FAILED sites_plugin:\tmake_webpattern_ads_sites()"
        raise
#test_make_webpattern_ads_sites()

def test_check_guid_format():
    try:
        sites = sites_plugin.SitesPlugin()
        guid_f = "/home/dev/Desktop/all_guids.txt"
        sites.check_guid_format(guid_f)
    except:
        print "FAILED sites_plugin:\tcheck_guid_format()"
        raise
#test_check_guid_format()

def test_check_rule_format():
    try:
        sites = sites_plugin.SitesPlugin()
        sites_f = "/home/dev/Desktop/all_sites_rules.txt"
        referer_f = "/home/dev/Desktop/all_referer_rules.txt"
        sites.check_rule_format(sites_f)
        sites.check_rule_format(referer_f)
    except:
        print "FAILED sites_plugin:\tcheck_rule_format()"
        raise
#test_check_rule_format()

def test_find_guid():
    try:
        sites = sites_plugin.SitesPlugin()
        guid_f = "/home/dev/Desktop/guids.txt"
        sites.find_guid(guid_f)
    except:
        print "FAILED sites_plugin:\tfind_guid()"
        raise
#test_find_guid()

"""

flowtool.py test cases

"""
pcap_path = test_base_floder_path + "/trace.pcap"
pcap_folder = test_base_floder_path

def test_run_flowtool():
    try:
        ft = flowtool.Flowtool()
        ft.run_flowtool(pcap_path)
    except:
        print "FAILED flowtool: run_flowtool()"
        raise
#test_run_flowtool()

def test_run_flowtool_folder():
    try:
        ft = flowtool.Flowtool()
        ft.run_flowtool_folder(pcap_folder)
    except:
        print "FAILED flowtool: run_flowtool_folder()"
        raise
#test_run_flowtool_folder()

"""

utility.py test cases

"""

def test_remove_postfix_slash():
    string = "/home/dev/test/"
    base = "/home/dev/test"
    try:
        ut = utility.Utility()
        result = ut.remove_postfix_slash(string)
        if result != base:
            print "FAILED utility:\tremove_postfix_slash()"
    except:
        print "FAILED utility:\tremove_postfix_slash()"
        raise
#test_remove_postfix_slash()

print "TEST FINISHED!"